package com.tg.paw.ums.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tg.paw.ums.entity.AdminRoleRelation;
import com.tg.paw.ums.entity.Role;
import com.tg.paw.ums.service.IAdminRoleRelationService;
import com.tg.paw.ums.service.IApiAdminService;
import com.tg.paw.ums.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Rubble
 * @date 2020/10/28
 */
@Slf4j
@Service
public class ApiAdminServiceImpl  implements IApiAdminService {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IAdminRoleRelationService adminRoleRelationService;

    @Override
    public List<Role> getAdminRolesEnabled(Long adminId) {
        List<AdminRoleRelation> adminRoleRelations = adminRoleRelationService.list(Wrappers.<AdminRoleRelation>lambdaQuery().eq(AdminRoleRelation::getAdminId, adminId));
        List<Long> roleIds = adminRoleRelations.stream().map(item->item.getRoleId()).collect(Collectors.toList());
        List<Role> roles = new ArrayList<>();
        if(CollUtil.isNotEmpty(roleIds)) {
            roles = roleService.list(Wrappers.<Role>lambdaQuery().in(Role::getRoleId, roleIds).eq(Role::getStatus, 1));
        }
        return roles;
    }
}
